package net.xumf.support.utils;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

public class VelocityParser {

    public static String process(String name, Map data) {
        VelocityEngine ve = new VelocityEngine();
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        ve.init();
        VelocityContext ctx = new VelocityContext(data);
        StringWriter sw = new StringWriter();
        Template tableTemplate = ve.getTemplate(name);
        ve.mergeTemplate(tableTemplate.getName(), "UTF-8", ctx, sw);
        return sw.toString();
    }

    public static void main(String[] args) {

        Map<String, Object> data = new HashMap<>();
        data.put("now", System.currentTimeMillis());
        String process = process("/templates/report/design-table.vm", data);
        System.out.println(process);
    }
}
